package com.kb.controller.crm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.common.ApiResponse;
import com.kb.common.MeException;
import com.kb.domain.WaitingInvoice;
import com.kb.dto.InvoiceDetailsDTO;
import com.kb.dto.InvoiceSearchDTO;
import com.kb.dto.WaitingInvoiceDTO;
import com.kb.entity.crm.CrmShipment;
import com.kb.entity.crm.Invoice;
import com.kb.entity.kbidm.*;
import com.kb.service.crm.KBEService;
import com.kb.service.crm.KBIDMService;
import com.kb.util.MessageUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/kbe")
@Api(value = "KBE订单中心", tags = {"KBE订单中心"})
@Slf4j
public class KBEController {

    @Resource
    private KBEService kbeService;
    @Resource
    private KBIDMService kbidmService;

    /**
     * 获取开票分页信息
     */
    @PostMapping("/queryInvoice")
    @ResponseBody
    @ApiOperation("获取开票分页信息")
    public Page<Invoice> queryInvoice(@RequestBody InvoiceSearchDTO invoiceSearchDTO) throws MeException {
        return kbeService.queryInvoice(invoiceSearchDTO);
    }

    /**
     * 获取开票信息
     */
    @GetMapping("/queryInvoiceById")
    @ResponseBody
    @ApiOperation("获取开票信息")
    public Invoice queryInvoice(String id) throws MeException {
        return kbeService.queryInvoiceById(id);
    }

    /**
     * 获取开票明细信息
     */
    @GetMapping("/queryInvoiceDetails")
    @ResponseBody
    @ApiOperation("获取开票明细信息")
    public List<InvoiceDetailsDTO> queryInvoiceDetails(String id) throws MeException {
        return kbeService.queryInvoiceDetails(id);
    }

    /**
     * 获取待开票信息
     */
    @PostMapping("/queryWaitingInvoice")
    @ResponseBody
    @ApiOperation("获取待开票信息")
    public Page<WaitingInvoice> queryWaitingInvoice(@RequestBody InvoiceSearchDTO invoiceSearchDTO) throws MeException {
        return kbeService.queryWaitingInvoice(invoiceSearchDTO);
    }

    /**
     * crm客户信息同步到订单中台
     */
    @PostMapping("/crmCustInfo2IDM")
    @XxlJob("crmCustInfo2IDM")
    @ResponseBody
    public ApiResponse crmCustInfo2IDM(@RequestBody List<String> codeList) throws MeException, ParseException {
        kbidmService.crmCustInfo2IDM(codeList);
        return ApiResponse.success("succeed");
    }
    /**
     * crm客户基本信息同步到订单中台
     */
    @PostMapping("/crmCustBaseInfo2IDM")
    @XxlJob("crmCustBaseInfo2IDM")
    @ResponseBody
    public ApiResponse crmCustBaseInfo2IDM(@RequestBody List<String> codeList) throws MeException, ParseException {
        kbidmService.crmCustBaseInfo2IDM(codeList);
        return ApiResponse.success("succeed");
    }

    /**
     * 订单中台客户信息同步到crm
     */
    @PostMapping("/kbeCustInfo2Crm")
    @ResponseBody
    public Long kbeCustInfo2Crm(@RequestBody KbSysCust customer) throws MeException {
        return kbidmService.kbeCustInfo2Crm(customer);
    }

    /**
     * 发票同步到crm
     */
    @PostMapping("/kbeInvoice2Crm")
    @ResponseBody
    public Long kbeInvoice2Crm(@RequestBody KbInvoicing invoicing) throws MeException {
        return kbidmService.kbeInvoice2Crm(invoicing);
    }

    /**
     * 地址同步到crm
     */
    @PostMapping("/kbeAddress2Crm")
    @ResponseBody
    public Long kbeAddress2Crm(@RequestBody KbCustAddress address) throws MeException {
        return kbidmService.kbeAddress2Crm(address);
    }

    /**
     * 联系人同步到crm
     */
    @PostMapping("/kbeContacts2Crm")
    @ResponseBody
    public Long kbeContacts2Crm(@RequestBody KbTechnicalContacts contacts) throws MeException {
        return kbidmService.kbeContacts2Crm(contacts);
    }

    /**
     * 申请开票
     *
     * @return
     */
    @PostMapping("/applyInvoicing")
    @ResponseBody
    public ApiResponse applyInvoicing(@RequestBody List<CrmShipment> shipments) throws MeException {
        kbeService.applyInvoicing(shipments);
        return ApiResponse.success("succeed");
    }
    /**
     * 新建客户
     *
     * @return
     */
    @PostMapping("/createCustomer")
    @ResponseBody
    public ApiResponse createCustomer(@RequestBody CustomerRequest customerRequest) throws MeException {
        return ApiResponse.success(kbeService.createCustomer(customerRequest));
    }
    /**
     * 获取最新客代
     *
     * @return
     */
    @GetMapping("/getCustomCode")
    @ResponseBody
    public ApiResponse getCustomCode() throws MeException {
        return ApiResponse.success(kbeService.getCustomCode());
    }
    /**
     * 获取销售人员
     *
     * @return
     */
    @GetMapping("/getSales")
    @ResponseBody
    public ApiResponse getSales() throws MeException {
        return ApiResponse.success(kbeService.getSales());
    }


//    @PostMapping("/test")
//    @ResponseBody
//    public void test() {
//        MessageUtil.sentMessage("23988", "这是测试数据，迎来测温大声道大大阿达阿达", "国创资源库");
//    }
}
